package view;

import java.util.ArrayList;
import java.util.TreeMap;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

import controller.AddWerkprocesPanelActions;

import model.kwalificatiebeheer.Competentie;
import model.kwalificatiebeheer.Criterium;
import model.kwalificatiebeheer.Kerntaak;
import model.kwalificatiebeheer.Uitstroomrichting;
import model.kwalificatiebeheer.Werkproces;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class AddWerkprocesFrame extends JFrame{
	
	private Werkproces wp;
	private Uitstroomrichting ur;
	private Kerntaak kt;	
	private JTextField txtTitle;
	private JTextArea txtOmschrijving;
	private JTextArea txtGewensteResultaat;
	private JTextArea txtVakkennisVaardigheden;
	private JTable tblCompetenties;
	private JPanel tablePanel;
	private JPanel formPanel;
	private ArrayList<Uitstroomrichting> urs;
	private JTable urTable;
	
	public AddWerkprocesFrame(Kerntaak kt){
		this.kt = kt;
		urs = new ArrayList<>(kt.getKd().getUitstroomrichtingen());
		initTable();
		setTitle("Werkproces Toevoegen");
		ImageIcon icon = new ImageIcon(this.getClass().getResource("../resources/favicon.png"));
		setIconImage(icon.getImage());
		setVisible(true);
		setSize(500, 500);
	}

	public void initTable(){
		tablePanel = new JPanel();
		GridBagLayout gbl_p = new GridBagLayout();
		gbl_p.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_p.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_p.columnWeights = new double[]{0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_p.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		tablePanel.setLayout(gbl_p);
		
		urTable = new JTable(new UitstroomrichtingTableModel(urs));
		urTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		urTable.addMouseListener(new AddWerkprocesPanelActions(this));
		
		JTableHeader header = urTable.getTableHeader();
		header.setReorderingAllowed(false);
		GridBagConstraints gbc_header = new GridBagConstraints();
		gbc_header.gridwidth = 2;
		gbc_header.insets = new Insets(0, 0, 0, 0);
		gbc_header.fill = GridBagConstraints.BOTH;
		gbc_header.gridx = 1;
		gbc_header.gridy = 1;
		tablePanel.add(header, gbc_header);
		
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.gridwidth = 2;
		gbc_table.insets = new Insets(0, 0, 5, 0);
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 1;
		gbc_table.gridy = 2;
		tablePanel.add(urTable, gbc_table);
				
		JButton btnSelecteren = new JButton("Selecteren");
		btnSelecteren.addActionListener(new AddWerkprocesPanelActions(this));
		btnSelecteren.setActionCommand("btnSelecteren");
		GridBagConstraints gbc_btnSelecteren = new GridBagConstraints();
		gbc_btnSelecteren.anchor = GridBagConstraints.EAST;
		gbc_btnSelecteren.insets = new Insets(0, 0, 5, 5);
		gbc_btnSelecteren.gridx = 2;
		gbc_btnSelecteren.gridy = 3;
		tablePanel.add(btnSelecteren, gbc_btnSelecteren);
		
		setContentPane(tablePanel);
		revalidate();
	}
	
	public void initForm(){
		formPanel = new JPanel();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 24, 30, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 14, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		formPanel.setLayout(gridBagLayout);
		
		JLabel lblTitle = new JLabel("Titel:");		
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblTitle.gridx = 1;
		gbc_lblTitle.gridy = 1;
		formPanel.add(lblTitle, gbc_lblTitle);
		
		txtTitle = new JTextField();
		GridBagConstraints gbc_txtTitle = new GridBagConstraints();
		gbc_txtTitle.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTitle.insets = new Insets(0, 0, 5, 5);
		gbc_txtTitle.anchor = GridBagConstraints.NORTH;
		gbc_txtTitle.gridx = 2;
		gbc_txtTitle.gridy = 1;
		formPanel.add(txtTitle, gbc_txtTitle);
		
		JLabel lblOmschrijving = new JLabel("Omschrijving:");		
		GridBagConstraints gbc_lblOmschrijving= new GridBagConstraints();
		gbc_lblOmschrijving.insets = new Insets(0, 0, 5, 5);
		gbc_lblOmschrijving.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblOmschrijving.gridx = 1;
		gbc_lblOmschrijving.gridy = 2;
		formPanel.add(lblOmschrijving, gbc_lblOmschrijving);
		
		txtOmschrijving = new JTextArea();
		GridBagConstraints gbc_txtOmschrijving = new GridBagConstraints();
		gbc_txtOmschrijving.fill = GridBagConstraints.BOTH;
		gbc_txtOmschrijving.insets = new Insets(0, 0, 5, 5);
		gbc_txtOmschrijving.gridx = 2;
		gbc_txtOmschrijving.gridy = 2;
		formPanel.add(txtOmschrijving, gbc_txtOmschrijving);
		
		JLabel lblGewensteResultaat = new JLabel("Gewenste resultaat:");
		GridBagConstraints gbc_lblGewensteResultaat = new GridBagConstraints();
		gbc_lblGewensteResultaat.anchor = GridBagConstraints.EAST;
		gbc_lblGewensteResultaat.insets = new Insets(0, 0, 5, 5);
		gbc_lblGewensteResultaat.gridx = 1;
		gbc_lblGewensteResultaat.gridy = 3;
		formPanel.add(lblGewensteResultaat, gbc_lblGewensteResultaat);
		
		txtGewensteResultaat = new JTextArea();
		GridBagConstraints gbc_txtGewensteResultaat = new GridBagConstraints();
		gbc_txtGewensteResultaat.fill = GridBagConstraints.BOTH;
		gbc_txtGewensteResultaat.insets = new Insets(0, 0, 5, 5);
		gbc_txtGewensteResultaat.gridx = 2;
		gbc_txtGewensteResultaat.gridy = 3;
		formPanel.add(txtGewensteResultaat, gbc_txtGewensteResultaat);
		
		JLabel lblVakkennisEnVaardigheden = new JLabel("Vakkennis en Vaardigheden:");
		GridBagConstraints gbc_lblVakkennisEnVaardigheden = new GridBagConstraints();
		gbc_lblVakkennisEnVaardigheden.anchor = GridBagConstraints.EAST;
		gbc_lblVakkennisEnVaardigheden.insets = new Insets(0, 0, 5, 5);
		gbc_lblVakkennisEnVaardigheden.gridx = 1;
		gbc_lblVakkennisEnVaardigheden.gridy = 4;
		formPanel.add(lblVakkennisEnVaardigheden, gbc_lblVakkennisEnVaardigheden);		
		
		txtVakkennisVaardigheden = new JTextArea();
		GridBagConstraints gbc_txtVakkennisVaardigheden = new GridBagConstraints();
		gbc_txtVakkennisVaardigheden.fill = GridBagConstraints.BOTH;
		gbc_txtVakkennisVaardigheden.insets = new Insets(0, 0, 5, 5);
		gbc_txtVakkennisVaardigheden.gridx = 2;
		gbc_txtVakkennisVaardigheden.gridy = 4;
		formPanel.add(txtVakkennisVaardigheden , gbc_txtVakkennisVaardigheden );
		
		JButton btnSave = new JButton("Opslaan");
		btnSave.addActionListener(new AddWerkprocesPanelActions(this));
		btnSave.setActionCommand("btnSave");
		GridBagConstraints gbc_btnAnnuleren = new GridBagConstraints();
		gbc_btnAnnuleren.anchor = GridBagConstraints.EAST;
		gbc_btnAnnuleren.insets = new Insets(0, 0, 5, 5);
		gbc_btnAnnuleren.gridx = 3;
		gbc_btnAnnuleren.gridy = 5;
		formPanel.add(btnSave, gbc_btnAnnuleren);		
		setContentPane(formPanel);
		revalidate();
	}

	public Werkproces getWp() {
		return wp;
	}

	public Kerntaak getKt() {
		return kt;
	}

	public JTextField getTxtTitle() {
		return txtTitle;
	}

	public JTextArea getTxtOmschrijving() {
		return txtOmschrijving;
	}

	public JTextArea getTxtGewensteResultaat() {
		return txtGewensteResultaat;
	}

	public JTextArea getTxtVakkennisVaardigheden() {
		return txtVakkennisVaardigheden;
	}

	public JTable getTblCompetenties() {
		return tblCompetenties;
	}

	public JPanel getTablePanel() {
		return tablePanel;
	}

	public JPanel getFormPanel() {
		return formPanel;
	}

	public JTable getUrTable() {
		return urTable;
	}

	public ArrayList<Uitstroomrichting> getUrs() {
		return urs;
	}

	public void setUrs(ArrayList<Uitstroomrichting> urs) {
		this.urs = urs;
	}

	public Uitstroomrichting getUr() {
		return ur;
	}

	public void setUr(Uitstroomrichting ur) {
		this.ur = ur;
	}
}
